package com.kaka6;

import java.sql.*;

public class TestMysql {
    public static void main(String[] args) throws Exception {
        //连接上数据库
        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_maven?useUnicode=true&characterEncoding=utf8",
                "root","");
        //准备sql语句
        String sql = "select id, username, age, birth from users";

        //预编译sql语句
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //执行sql语句
        ResultSet rs = pstmt.executeQuery();

        //使用循环的方式从rs中获取数据
        while (rs.next()){
            System.out.print(rs.getInt("id") + "\t");
            System.out.print(rs.getString("username") + "\t");
            System.out.print(rs.getInt("age") + "\t");
            System.out.print(rs.getTimestamp("birth") + "\t");
            System.out.println();
        }
    }
}
